<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller
{
    //
    use AuthenticatesUsers;

    public function __construct()
    {
        $this->middleware('guest:admin')->except('logout');
    }

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected  $redirectTo = '/admin/home';


    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    function  showLoginForm()
    {
        return  view('admin.auth.login');
    }



    protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
            'captcha' => ['required', 'captcha'],
        ],[
            'captcha.required' => '请填写验证码',
            'captcha.captcha' => '验证码错误',
        ]);
    }


    protected   function sendFailedLoginResponse(){

        throw ValidationException::withMessages([
            $this->username() => [trans('用户名或密码错误')],
        ]);
    }

    function  username(){

        return   'name';
    }


    protected  function  guard()
    {
        return  Auth::guard('admin');
    }

    protected  function  authenticated(Request $request, $user)
    {
        return   user_business_handler()->success(null,'登录成功，请稍后……');
    }

    protected   function loggedOut(){

        return   redirect()->route('admin.login');
    }


}
